package org.dromara.demo.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.RequiredArgsConstructor;
import org.dromara.common.core.utils.MapstructUtils;
import org.dromara.common.core.utils.StringUtils;
import org.dromara.common.mybatis.core.page.PageQuery;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.dromara.common.web.core.BaseService;
import org.dromara.demo.domain.TestDemo2;
import org.dromara.demo.domain.TestDemo2Line;
import org.dromara.demo.domain.bo.TestDemo2Bo;
import org.dromara.demo.domain.bo.TestDemo2LineBo;
import org.dromara.demo.domain.vo.TestDemo2LineVo;
import org.dromara.demo.domain.vo.TestDemo2Vo;
import org.dromara.demo.mapper.TestDemo2LineMapper;
import org.dromara.demo.mapper.TestDemo2Mapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.Collection;
import java.util.List;
import java.util.Map;

/**
 * 测试主从Service业务层处理
 *
 * @author wenrui Su
 * @date 2024-03-24
 */
@RequiredArgsConstructor
@Service
public class TestDemo2ServiceImpl extends BaseService {

  private final TestDemo2Mapper baseMapper;

  private final TestDemo2LineMapper lineMapper;

  /**
   * 获取所有主表数据
   */
  public List<TestDemo2Vo> list() {
    List<TestDemo2> list = baseMapper.selectList();
    return MapstructUtils.convert(list, TestDemo2Vo.class);
  }

  /**
   * 查询测试主从
   */
  public TestDemo2Vo queryById(Long id) {
    return baseMapper.selectVoById(id);
  }

  /**
   * 查询测试主从列表
   */
  public TableDataInfo<TestDemo2Vo> queryPageList(TestDemo2Bo bo, PageQuery pageQuery) {
    LambdaQueryWrapper<TestDemo2> lqw = buildQueryWrapper(bo);
    Page<TestDemo2Vo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
    return TableDataInfo.build(result);
  }

  /**
   * 查询测试主从列表
   */
  public List<TestDemo2Vo> queryList(TestDemo2Bo bo) {
    LambdaQueryWrapper<TestDemo2> lqw = buildQueryWrapper(bo);
    return baseMapper.selectVoList(lqw);
  }

  private LambdaQueryWrapper<TestDemo2> buildQueryWrapper(TestDemo2Bo bo) {
    Map<String, Object> params = bo.getParams();
    LambdaQueryWrapper<TestDemo2> lqw = Wrappers.lambdaQuery();
    lqw.like(StringUtils.isNotBlank(bo.getName()), TestDemo2::getName, bo.getName());
    lqw.eq(bo.getDeptId() != null, TestDemo2::getDeptId, bo.getDeptId());
    lqw.eq(bo.getUserId() != null, TestDemo2::getUserId, bo.getUserId());
    return lqw;
  }

  /**
   * 新增测试主从
   */
  @Transactional(propagation = Propagation.NESTED)
  public Boolean insertByBo(TestDemo2Bo bo) {
//    新增主表
    TestDemo2 add = MapstructUtils.convert(bo, TestDemo2.class);
    validEntityBeforeSave(add);
    boolean flag = baseMapper.insert(add) > 0;
    if (flag) {
      bo.setId(add.getId());
    }
//  新增子表
    List<TestDemo2LineBo> lineList = bo.getLineList();
    for (TestDemo2LineBo line : lineList) {
      line.setId(bo.getId());
      this.insertLineByBo(line);
    }

    return flag;
  }

  /**
   * 修改测试主从
   */
  @Transactional(propagation = Propagation.NESTED)
  public Boolean updateByBo(TestDemo2Bo bo) {
    TestDemo2 update = MapstructUtils.convert(bo, TestDemo2.class);
    validEntityBeforeSave(update);

    List<TestDemo2LineBo> lineList = bo.getLineList();
    for (TestDemo2LineBo line : lineList) {
      if (line.getLineId() == null) {
        line.setId(bo.getId());
        this.insertLineByBo(line); // 子表新增
      } else {
        this.updateLineByBo(line); // 子表修改
      }
    }

    return baseMapper.updateById(update) > 0;
  }

  /**
   * 保存前的数据校验
   */
  private void validEntityBeforeSave(TestDemo2 entity) {
    //TODO 做一些数据校验,如唯一约束
  }

  /**
   * 批量删除测试主从
   */
  @Transactional(propagation = Propagation.NESTED)
  public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
    if (isValid) {
      //TODO 做一些业务上的校验,判断是否需要校验
    }
    this.deleteLineWithValidByIds(ids, true);
    return baseMapper.deleteBatchIds(ids) > 0;
  }

  /**
   * 查询测试从========================================操作line表开始===============================
   */
//  public TestDemo2LineVo queryLineById(Long lineId) {
//    return  lineMapper.selectVoById(lineId,TestDemo2LineVo.class);
//  }

  /**
   * 查询测试从列表
   */
  public TableDataInfo<TestDemo2LineVo> queryLinePageList(TestDemo2LineBo bo, PageQuery pageQuery) {
    LambdaQueryWrapper<TestDemo2Line> lqw = buildLineQueryWrapper(bo);
    Page<TestDemo2LineVo> result = lineMapper.selectVoPage(pageQuery.build(), lqw);
    return TableDataInfo.build(result);
  }

  private LambdaQueryWrapper<TestDemo2Line> buildLineQueryWrapper(TestDemo2LineBo bo) {
    LambdaQueryWrapper<TestDemo2Line> lqw = Wrappers.lambdaQuery();
    lqw.eq(bo.getId() != null, TestDemo2Line::getId, bo.getId());
    return lqw;
  }

  /**
   * 新增测试从
   */
  @Transactional(propagation = Propagation.REQUIRED)
  public Boolean insertLineByBo(TestDemo2LineBo bo) {
    TestDemo2Line add = MapstructUtils.convert(bo, TestDemo2Line.class);
    validLineEntityBeforeSave(add);
    boolean flag = lineMapper.insert(add) > 0;
    if (flag) {
      bo.setLineId(add.getLineId());
    }
    return flag;
  }

  /**
   * 修改测试从
   */
  @Transactional(propagation = Propagation.REQUIRED)
  public Boolean updateLineByBo(TestDemo2LineBo bo) {
    TestDemo2Line update = MapstructUtils.convert(bo, TestDemo2Line.class);
    validLineEntityBeforeSave(update);
    return lineMapper.updateById(update) > 0;
  }

  /**
   * 保存前的数据校验
   */
  private void validLineEntityBeforeSave(TestDemo2Line entity) {
    //TODO 做一些数据校验,如唯一约束
  }

  /**
   * 批量删除通过子表lineId
   */
  @Transactional(propagation = Propagation.REQUIRED)
  public Boolean deleteWithValidByLineIds(Collection<Long> lineIds, Boolean isValid) {
    if (isValid) {
      //TODO 做一些业务上的校验,判断是否需要校验
    }
    return lineMapper.deleteBatchIds(lineIds) > 0;
  }

  /**
   * 通过主表ids删除子表
   *
   * @param ids
   * @param isValid
   * @return
   */
  @Transactional(propagation = Propagation.REQUIRED)
  public Boolean deleteLineWithValidByIds(Collection<Long> ids, Boolean isValid) {
    if (isValid) {
      //TODO 做一些业务上的校验,判断是否需要校验
    }
    LambdaQueryWrapper<TestDemo2Line> lqw = new LambdaQueryWrapper<>();
    lqw.in(TestDemo2Line::getId, ids);
    return lineMapper.delete(lqw) > 0;
  }
}
